<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 17-2-23
 * Time: 下午2:07
 */

namespace Cframe;

class Route {
    private static $uri = '';
    private static $r_method = '';
    private static $routes = array();
    private static $replace = array();
    private static $pathinfo = '';
    private static $fun = '';
    public function __construct(){
        if (isset($_SERVER['PATH_INFO'])){
            static::$uri = $_SERVER['PATH_INFO'];
        }else{
            static::$uri = '';
        }
        static::$r_method = strtolower($_SERVER["REQUEST_METHOD"]);
        static::$fun = new Url();
    }

    public static function route(){
        static::$fun->registerRoute(static::$routes);
        $route = static::$uri;
        if (empty($route)){
            $url = [
                "class"=>'\\'.CON_NAME.'\\'.BIND_MODULE."\\".APP_NAME."\\".BIND_CONTROLLER,
                "func"=>BIND_ACTION,
                "method"=>["get","post"]
            ];
        }else{
            $url = $route;
            $url = static::$fun->loadUrl($url);
        }
        $method = (!empty($url['method']))?$url['method']:array(static::$r_method);
        if (in_array(static::$r_method,$method)){
            $class = $url['class'];
            $function = $url['func'];
            $array = explode("\\",$class);
            if (!empty($class) && class_exists($class)) {
                define("MODULE_NAME",$array[2]);
                define("CONTROLLER_NAME",end($array));
                define("ACTION_NAME",$function);
                $data = call_user_func(array(new $class,$function));
            }elseif(empty($class) && is_callable($function)){
                $data = call_user_func($function);
            }else{
                if (APP_DEBUG){
                    throw new RouteException(" " . str_replace("\\Controller",'',$class) . "\\{$function}" . " 不存在");
                }else{
                    header("HTTP/1.1 404 Not Found");
                    $urls = str_replace("index.php/",'',static::$uri);
                    exit("<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL  ".$urls." was not found on this server.</p><hr><address>Apache Server at localhost Port 80</address></body></html>");
                }
            }
        }else{
            header("HTTP/1.1 403 Forbidden");
            $urls = str_replace("index.php",'',static::$uri);
            exit("<html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don't have permission to access ".$urls." on this server.<br />Server unable to read htaccess file, denying access to be safe</p><hr><address>Apache Server at localhost Port 80</address></body></html>");
        }
        return $data;
    }

    public static function __callStatic($name, $arguments){
        static::$routes = array_merge(static::$routes,array($arguments[0]=>array([$name],$arguments[1])));
    }

    public function __destruct(){
        $data = self::route();
        if (is_a($data,"\\Cframe\\Response")){
            $data->send();
        }else{
            $a = new Response($data);
            // 设置编码
            $a->contentType("text/html")->send();
        }
        if (APP_DEBUG){
            Log::debug();
        }
    }
}

class RouteException extends \Exception{

}